package org.pvt.model;

import android.content.Context;
import android.database.Cursor;
import org.jetbrains.annotations.NotNull;
import org.pvt.model.data.IdAndNameDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Pavel Talanov
 */
public final class Queries {

    @NotNull
    public static List<IdAndNameDTO> getIdToName(@NotNull HasNameAndBelongsToEvent model, long eventId, @NotNull Context context) {
        List<IdAndNameDTO> idToName = new ArrayList<IdAndNameDTO>();
        Cursor cursor = model.getCursor(eventId, context);
        assert (cursor.getCount() > 0);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            long id = cursor.getLong(cursor.getColumnIndex(model.ID));
            String name = cursor.getString(cursor.getColumnIndex(model.NAME));
            idToName.add(new IdAndNameDTO(id, name));
            cursor.moveToNext();
        }
        cursor.close();
        return idToName;
    }

    @NotNull
    public static String[] getNamesInTheSameOrder(@NotNull List<IdAndNameDTO> idToName) {
        List<String> names = new ArrayList<String>();
        for (IdAndNameDTO idAndName : idToName) {
            names.add(idAndName.name);
        }
        return names.toArray(new String[names.size()]);
    }
}
